#!/usr/bin/env bash

function _hcli_complete_func () {
  local PARAM0 cur opts cmd
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  now="${COMP_LINE##* }"
  cmd="${COMP_WORDS[COMP_CWORD-1]}"

  PARAM_REGION="$(ls $HOME/deploy/hcli/*-hosts | grep -v ":")"
  PARAM_COMMAND="$(cat $HOME/deploy/hcli/hcli | grep "# param:command" | awk '{print $1}' | grep -oP '\S+(?=\s*\))' | tr '\n|' ' ')"
  PARAM_SERVICE="$(cat $HOME/deploy/hcli/hcli | grep "# param:service" | awk '{print $1}' | grep -oP '\S+(?=\s*\))' | tr '\n|' ' ')"
  PARAM_SORT="$(cat $HOME/deploy/hcli/hcli | grep "# param:-" | awk '{print $1}' | grep -oP '\S+(?=\s*\))' | tr '\n|' ' ')"
  PARAM_NUMBER="$(cat $HOME/deploy/hcli/*-hosts/* | grep -v "^#" | grep -v "^$" | wc -l | { read count; for((i=0;i<$count;i++));do echo -n "$i "; done })"
  case "${cmd}" in
  -r)
    opts="$PARAM_REGION"
    ;;
  -c)
    opts="$PARAM_COMMAND"
    ;;
  -s)
    opts="$PARAM_SERVICE"
    ;;
  -n)
    opts="$PARAM_NUMBER"
    ;;
  -t)
    ;;
  *)
    opts="$PARAM_SORT"
    ;;
  esac
  COMPREPLY=($(compgen -W "$opts" -- "$cur"))
  return 0
}
# -o bashdefault -o nospace
complete -o default -F _hcli_complete_func hcli
